package com.zsr.example.util;

import com.zsr.example.util.model.PageParam;
import com.zsr.example.vo.CommentVo;
import org.apache.commons.collections4.ListUtils;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;

/**
 * 手动分页
 *
 * @author zhousiren
 * @date 2022/3/24 14:52
 */
public class PageUtil {
    /**
     * 分页
     *
     * @param pageNum    当前页
     * @param pageSize   每页个数
     * @param originList 源列表
     * @return
     */
    public static <T> List<T> build(int pageNum, int pageSize, List<T> originList) {
        int total = originList.size();
        if (pageSize > total) return originList;

        List<T> records = null;
        int pageTotal = (total + pageSize - 1) / pageSize;//总页数
        if (pageNum <= pageTotal) {
            List<List<T>> partition = ListUtils.partition(originList, pageSize);
            records = partition.get((pageNum - 1));
        }

        return records;
    }

    /**
     * 分页
     *
     * @param pageParam
     * @param originList
     * @param <T>
     * @return
     */
    public static <T> List<T> build(PageParam pageParam, List<T> originList) {
        return build(pageParam.getPageNum(), pageParam.getPageSize(), originList);
    }
}
